package dao;

// Generated Mar 18, 2014 10:39:39 PM by Hibernate Tools 3.4.0.CR1
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import pojo.Mobile;
import pojo.User;
import pojo.UserHasMobile;
import pojo.UserHasMobileId;
import util.HibernateUtil;

/**
 * Home object for domain model class UserHasMobile.
 *
 * @see pojo.UserHasMobile
 * @author Hibernate Tools
 */
public class UserHasMobileHome {

    private final Session session;

    public UserHasMobileHome(SessionFactory sessionFactory) {
        session = sessionFactory.openSession();
    }

    public void persist(UserHasMobile transientInstance) {
        session.beginTransaction();
        session.persist(transientInstance);
        session.getTransaction().commit();
    }

    public void delete(UserHasMobile persistentInstance) {
        session.beginTransaction();
        session.delete(persistentInstance);
        session.getTransaction().commit();
    }

//    public UserHasMobile merge(UserHasMobile detachedInstance) {
//        log.debug("merging UserHasMobile instance");
//        try {
//            UserHasMobile result = (UserHasMobile) sessionFactory
//                    .getCurrentSession().merge(detachedInstance);
//            log.debug("merge successful");
//            return result;
//        } catch (RuntimeException re) {
//            log.error("merge failed", re);
//            throw re;
//        }
//    }
//
    public UserHasMobile findById(int id) {
        System.out.println(id);
        try {
            UserHasMobile instance = (UserHasMobile) session.get("pojo.UserHasMobile", id);
            if (instance == null) {
                System.out.println("get successful, no instance found");
            } else {
//                log.debug("get successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
//            log.error("get failed", re);
            throw re;
        }
    }
//
//    public List findByExample(UserHasMobile instance) {
//        log.debug("finding UserHasMobile instance by example");
//        try {
//            List results = sessionFactory.getCurrentSession()
//                    .createCriteria("pojo.UserHasMobile")
//                    .add(Example.create(instance)).list();
//            log.debug("find by example successful, result size: "
//                    + results.size());
//            return results;
//        } catch (RuntimeException re) {
//            log.error("find by example failed", re);
//            throw re;
//        }
//    }

    public void addUserHasMobile(UserHasMobile userHasMobile) {
        session.beginTransaction();
        session.persist(userHasMobile);
        session.getTransaction().commit();
    }

    public void updateUserHasMobile(UserHasMobile userHasMobile) {
        session.beginTransaction();
        session.update(userHasMobile);
        session.getTransaction().commit();
    }

    public UserHasMobile findByCompositeId(UserHasMobileId id) {
//        List l = session.createCriteria(UserHasMobile.class)
//                .add(Restrictions.eq("userId", id.getUserId()))
//                .add(Restrictions.eq("mobileId", id.getMobileId()))
//                .list();
            
            UserHome userDao = HibernateUtil.getUserDAO();
            User user = userDao.getUserById(id.getUserId());
            MobileHome mobileDao = HibernateUtil.getMobileDAO();
            Mobile mobile = mobileDao.getMobileById(id.getMobileId());

        List l = session.createCriteria(UserHasMobile.class)
                .add(Restrictions.eq("user", user))
                .add(Restrictions.eq("mobile", mobile))
                .list();

        return (UserHasMobile) l.get(0);
    }

    public void checkOutPhones(ArrayList<UserHasMobileId> checkedOutPhones) {
        for (UserHasMobileId id : checkedOutPhones) {
            UserHasMobile userHasMobile = findByCompositeId(id);
            userHasMobile.setCheckedOut(true);
            updateUserHasMobile(userHasMobile);
        }
    }
    
    public List getHistory() {
        return session.createCriteria(UserHasMobile.class)
                .add(Restrictions.eq("checkedOut", true))
                .list();
    }
}
